/**
 * @file field
 * @author fanxueliang@qq.com
 */
import type {UnionSpecItem, LikeFieldType} from '../interface';
import {createStringField} from './stringField';
import {createColorField} from './colorField';
import {createBoolField} from './boolField';
import {createNumberField} from './numberField';
import {createEnumField} from './enumField';
import {createImageUploaderField} from './imageUploaderField';
import {createVideoUploaderField} from './videoUploaderField';
import {createAudioUploaderField} from './audioUploaderField';
import {createRichTextField} from './richTextField';
import {createUrlField} from './urlField';
import {createObjectField} from './objectField';
import {creatListField} from './listField';
import {createAlternativeField} from './alternativeField';

export function createFieldsModel(spec: UnionSpecItem, index: number, prefix: string): LikeFieldType {
    const dataType = spec.dataType;
    let fields;
    switch (dataType) {
        case 'STRING':
            fields = createStringField(spec, index, prefix);
            break;
        case 'COLOR':
            fields = createColorField(spec, index, prefix);
            break;
        case 'BOOL':
            fields = createBoolField(spec, index, prefix);
            break;
        case 'NUMBER':
            fields = createNumberField(spec, index, prefix);
            break;
        case 'ENUM':
            fields = createEnumField(spec, index, prefix);
            break;
        case 'UPLOAD':
            fields = createImageUploaderField(spec, index, prefix);
            break;
        case 'VIDEO':
            fields = createVideoUploaderField(spec, index, prefix);
            break;
        case 'AUDIO':
            fields = createAudioUploaderField(spec, index, prefix);
            break;
        case 'HTML':
            fields = createRichTextField(spec, index, prefix);
            break;
        case 'URL':
            fields = createUrlField(spec, index, prefix);
            break;
        case 'OBJECT':
            fields = createObjectField(spec, index, prefix);
            break;
        case 'LIST':
            fields = creatListField(spec, index, prefix);
            break;
        case 'ALTERNATIVE':
            fields = createAlternativeField(spec, index, prefix);
            break;
        default:
            throw new Error(`dataType ${dataType} is unknown!`);
    }
    return fields;
}

export function createFormModel(spec: UnionSpecItem[]) {
    return spec.map((item, index) => createFieldsModel(item, index, item.name));
}
